/**
 * Copyright [2020] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.standard.webflux.web.rest;

import com.alibaba.fastjson.JSONObject;
import com.redis.S;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;

/**
 * @project-name:standard-webflux
 * @package-name:com.hyts.standard.webflux.web.rest
 * @author:LiBo/Alex
 * @create-date:2022-08-12 21:28
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "lark", name = "notify.switch", havingValue = "true", matchIfMissing = false)
public class ScheduleLarkNotify  {

    /**
     * restTemplate请求服务
     */
    static RestTemplate restTemplate = new RestTemplate();

    /**
     * 初始化方法
     */
    @PostConstruct
    public void init() {
        log.info(" | -------------------------------------------------------------------- |");
        log.info(" | scheduleLarkNotify is inited will be execute the lark notify message |");
        log.info(" | -------------------------------------------------------------------- |");
    }

    /**
     * 通知消息方法
     */
    @Scheduled(cron = "0 47 21 ? * FRI")
    public void notifyMessage() {
        try{
            String url = "https://open.larksuite.com/open-apis/bot/v2/hook/fbd36f4c-c8d4-49ee-9715-c87b087e85eb";
            String message = "<at user_id=\"all\">所有人</at> 本周周报如下，请大家尽快填写。\uD83D\uDC4A\uD83C\uDFFB This week's weekly report is as follows, please fill in as soon as possible.加油 \n " + "2022 R&D Weekly Report研发周报看板 (https://wiz-ai.larksuite.com/sheets/shtuswF4SLA3cW5a5HjEFgzB99Z?sheet=6af2fa)";
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json");
            headers.setContentType(type);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("msg_type", "text");
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("text", message);
            jsonObject.put("content", jsonObject2);
            HttpEntity<JSONObject> entity = new HttpEntity<>(jsonObject, headers);
            log.info(" | -------------------------------------------------------------------- |");
            log.info("notifyMessage is to be execute url:{} process message:{},content:{}",url,message,
                    JSONObject.toJSONString(jsonObject,true));
            log.info(" | -------------------------------------------------------------------- |");
            log.info("execute the notify lark info:{}", restTemplate.postForObject(url, entity, JSONObject.class));
        }catch (Exception e){
            log.error("notifyMessage is failure!",e);
        }
    }

    public static void main(String[] args) {
        String str = "https://wiz-ai.larksuite.com/sheets/shtusWAzDU9YTlx5WrCBFWZIRPc?table=tblyunwofxtUPeY9&view=vewuCREkVn";
        System.out.println(restTemplate.getForObject(str, String.class));
    }

}
